﻿package com.pandemicode.framework.utils {
	/**
	* @author Michael Lawrence
	* @version 1.0.0
	*/
	public class StringHelper {
		public function StringHelper() { }
		
		/**
		 * Applies a format to a string.
		 * @param	format		The format string
		 * @param	...params	The objects to be inserted
		 * @return	Returns the formatted and substituted string.
		 */
		public static function ApplyFormat(format:String, ...params):String {
			for (var i:Number = 0; i < params.length; i++) {
				var q:String = "{" + i + "}";
				while(format.indexOf(q)>=0)
					format = format.replace(q, params[i]);
			}
			return format;
		}
		
		/**
		 * Returns a string that is comma separated version of the specified number.
		 * @param	n	The number to convert.
		 * @return	Returns a string that is comma separated version of the specified number.
		 */
		public static function AddCommas(n:Number):String {
			var x:Array = n.toString().split(".");
			var x1:String = x[0];
			var x2:String = x.length > 1 ? "." + x[1] : "";
			var rgx:RegExp = new RegExp("(\\d+)(\\d{3})");
			while (rgx.test(x1))
				x1 = x1.replace(rgx, "$1" + "," + "$2");
			return x1 + x2;
		}
		
		/**
		 * Returns a string that has had the double line breaks replaced with a single line break.
		 * @param	s	The string to replace.
		 * @return	Returns a string that has had the double line breaks replaced with a single line break.
		 */
		public static function RemoveDoubleBreaks(s:String):String {
			return s.replace("\r\n", "\n");
		}
	}
}